import { request, baseURL, type Response } from "@/utils/request";
import { type AxiosRequestConfig } from "axios";

export interface User {
    uid: string;
    code: string;
}
export interface Conversation {
    id: string;
    name: string;
    userId: string;
}
export interface Message {
    id?: string;
    role: string;
    content: string;
    reasoningContent?: string;
    conversationId?: string;
}
let { s } = window as any;
export default {
    // 游客登录
    userVisitorLogin(data?: { uid: string; verifyParam: string }) {
        return request({
            url: "/user/visitorLogin",
            data: { ...data, ...s({ uid: data.uid }) },
            method: "post",
        }).then((res) => res.data as Response<User>);
    },
    // 获取用户信息
    userInfo(params?: { uid: string }) {
        return request({
            url: "/user/info",
            params,
        }).then((res) => res.data as Response<User>);
    },
    // 根据连接码获取用户uid
    async userGetUid(params: { code: string }) {
        return await request({
            url: "/user/getUid",
            params,
        }).then((res) => res.data as Response<{ uid: string; isOnline: boolean }>);
    },

    // 会话创建
    conversationAdd() {
        return request({
            url: "/conversation/add",
            method: "post",
            data: s(),
        }).then((res) => res.data as Response<Conversation>);
    },
    // 会话列表
    conversationList() {
        return request({
            url: "/conversation/list",
        }).then((res) => res.data as Response<Conversation[]>);
    },
    // 会话重命名
    conversationRename(data: { id: string; content: string }) {
        return request({
            url: "/conversation/rename",
            method: "post",
            data: s(data),
        }).then((res) => res.data as Response<string>);
    },
    // 会话删除
    conversationDelete(data: { id: string }) {
        return request({
            url: "/conversation/delete",
            method: "post",
            data: s(data),
        }).then((res) => res.data);
    },

    // 消息列表
    messageList(params: { conversationId: string }) {
        return request({
            url: "/message/list",
            params,
        }).then((res) => res.data as Response<Message[]>);
    },
    // 消息对话
    messageChat(data: { content: string; conversationId: string }) {
        return fetch(`${baseURL}/message/chat`, {
            method: "POST",
            headers: { "Content-Type": "application/json" },
            body: JSON.stringify(s(data)),
        });
    },
};
